package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelCommendQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelContentService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

/**
 * Created by admin on 2019/8/11.
 */
@Controller
@RequestMapping("travel")
public class TravelController {

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelContentService travelContentService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelCommentService commentService;

    @Reference
    private ITravelStatisRedisService travelStatisRedisService;

    @RequestMapping("")
    public String list(Model model, @ModelAttribute("qo") TravelQuery qo, UserInfo userInfo) {

        if (qo.getState() == -1) {
            // 查询公开的已发布的游记
            qo.setState(Travel.STATE_RELEASE);
            qo.setIsPublic(true);
        }
        PageInfo list = travelService.query(qo);
        model.addAttribute("pageInfo", list);

        model.addAttribute("userInfo", userInfo);

        model.addAttribute("tcs", commentService.list());

        return "travel/list";
    }

    @RequestMapping("detail")
    public String detail(Model model, Long id, @UserParam UserInfo userInfo) {

        Travel detail = travelService.get(id);
        detail.setTravelContent(travelContentService.get(id));
        model.addAttribute("detail", detail);

        // toasts
        List<Destination> list = destinationService.queryForParent(detail.getDest().getId());
        model.addAttribute("toasts", list);

        // sds
        List<StrategyDetail> sds = strategyDetailService.getViewNumTop3(detail.getDest().getId());
        model.addAttribute("sds", sds);

        // ts
        List<Travel> ts = travelService.getViewNumTop3(detail.getDest().getId());
        model.addAttribute("ts", ts);

        // 游记阅读量
        travelStatisRedisService.viewNumIncrease(id, 1);
        TravelStatisVO vo = travelStatisRedisService.getTravelStatisVo(id);
        model.addAttribute("vo", vo);

        List<Long> c = travelStatisRedisService.getTravelStatisFavor(id);
        model.addAttribute("c",c);

        // userInfo
        model.addAttribute("userInfo", userInfo);

        TravelCommentQuery qo = new TravelCommentQuery();
        qo.setTravelId(id);
        qo.setCurrentPage(1);
        qo.setPageSize(Integer.MAX_VALUE);
        Page page = commentService.query(qo);
        model.addAttribute("list", page.getContent());

        return "travel/detail";
    }

    @RequireLogin
    @RequestMapping("input")
    public String input(Model model, Long id) {

        if (id != null) {
            Travel travel = travelService.get(id);
            travel.setTravelContent(travelContentService.get(id));
            model.addAttribute("tv", travel);
        }

        List<Destination> dests = destinationService.queryForList(3);
        model.addAttribute("dests", dests);

        return "travel/input";
    }

    @RequireLogin
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo) {
        travel.setAuthor(userInfo);
        Long id = travelService.saveOrUpdate(travel);
        return AjaxResult.SUCCESS.getData(id);
    }

    @RequestMapping("/commentAdd")
    public String commentAdd(Model model, TravelComment comment, int floor, @UserParam UserInfo userInfo) {

        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());

        TravelComment c = commentService.saveOrUpdate(comment);

        model.addAttribute("c", c);
        model.addAttribute("floor", floor + 1);

        return "travel/commentTpl";
    }


    @RequestMapping("travelThumbup")
    @ResponseBody
    public Object travelThumbup(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }

        boolean ret = travelStatisRedisService.travelThumbup(sid, userInfo.getId());

        TravelStatisVO vo = travelStatisRedisService.getTravelStatisVo(sid);

        return new AjaxResult(ret,"").getData(vo);
    }

    @RequestMapping("favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {

        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(102);
            return result;
        }
        boolean ret = travelStatisRedisService.favor(sid, userInfo.getId());

        TravelStatisVO vo = travelStatisRedisService.getTravelStatisVo(sid);

        return new AjaxResult(ret).getData(vo);
    }

    /**
     * 分享加分
     *
     * @param sid
     * @param model
     * @return
     */
    @RequestMapping("/share")
    @ResponseBody
    @RequireLogin
    public Object share(Long sid, Model model) {
        travelStatisRedisService.SharenumIncrease(sid, 1);
        return AjaxResult.SUCCESS;
    }

}
